package com.inei.cie.capituloi;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloIDao;
import com.inei.cie.entidades.P1_A_2_8N;
import com.inei.cie.entidades.P1_A_2_9N;
import com.inei.cie.utilitarios.Variables;

public class CapituloI_SeccionA_Modular_Activity extends Activity {

	// Variables
	public Variables var;
	public String id_local;
	public CapituloIDao capitulo;
	public int Nro_Pred;
	public int Nro_IE;
	public int nro;
	public Bundle b;
	public P1_A_2_8N obj;
	public List<String> lPMod;

	// Componentes
	public CheckBox cbOtro;
	public TextView tvTitulo;
	public Spinner sp1A9c, sp1A9d, sp1A9e, spModulares;
	public EditText et1A9a, et1A9b, et1A9f, et1A9g, et1A9h, et1A9i, et1A9j,
			et1A9k, et1A9l;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_i_seccion_a_modular);

		IniciarComponentes();

		cbOtro.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				if (cbOtro.isChecked()) {
					spModulares.setSelection(0);
					spModulares.setEnabled(false);
					et1A9a.setEnabled(true);
					et1A9a.requestFocus();
				} else {
					spModulares.setEnabled(true);
					et1A9a.setText("");
					et1A9a.setEnabled(false);
				}
			}
		});

		sp1A9c.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				if (pos == 5) {
					sp1A9d.setSelection(1);
					sp1A9d.setEnabled(false);
				} else {
					sp1A9d.setEnabled(true);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		sp1A9e.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				if (pos == 1) {
					et1A9f.setEnabled(true);
					et1A9f.requestFocus();
				} else {
					et1A9f.setEnabled(false);
					et1A9f.setText("");
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	public void IniciarComponentes() {
		tvTitulo = (TextView) findViewById(R.id.tvTitulo);

		cbOtro = (CheckBox) findViewById(R.id.cbOtro);

		sp1A9c = (Spinner) findViewById(R.id.sp1A9c);
		sp1A9d = (Spinner) findViewById(R.id.sp1A9d);
		sp1A9e = (Spinner) findViewById(R.id.sp1A9e);
		spModulares = (Spinner) findViewById(R.id.spModulares);

		et1A9a = (EditText) findViewById(R.id.et1A9a);
		et1A9b = (EditText) findViewById(R.id.et1A9b);
		et1A9f = (EditText) findViewById(R.id.et1A9f);
		et1A9g = (EditText) findViewById(R.id.et1A9g);
		et1A9h = (EditText) findViewById(R.id.et1A9h);
		et1A9i = (EditText) findViewById(R.id.et1A9i);
		et1A9j = (EditText) findViewById(R.id.et1A9j);
		et1A9k = (EditText) findViewById(R.id.et1A9k);
		et1A9l = (EditText) findViewById(R.id.et1A9l);

		CargarData();
	}

	public void CargarData() {
		capitulo = new CapituloIDao(this);
		var = new Variables();
		b = getIntent().getExtras();
		id_local = b.getString(var.ID_LOCAL);
		Nro_Pred = b.getInt(var.NRO_PRED);
		Nro_IE = b.getInt(var.NRO_IE);
		nro = b.getInt("nro_c");

		tvTitulo.setText(tvTitulo.getText() + " " + nro);

		// lPMod = new ArrayList<String>();
		lPMod = capitulo.ListadoModulares(id_local);
		if (lPMod == null) {
			lPMod = new ArrayList<String>();
		}
		lPMod.add(0, "Seleccionar C\u00f3digo");
		// lPMod.addAll(capitulo.ListadoModulares(id_local));
		ArrayAdapter<String> adapterMod = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, lPMod);
		spModulares.setAdapter(adapterMod);

		et1A9g.setNextFocusDownId(R.id.et1A9h);
		et1A9h.setNextFocusDownId(R.id.et1A9i);
		et1A9i.setNextFocusDownId(R.id.et1A9j);
		et1A9j.setNextFocusDownId(R.id.et1A9k);
		et1A9k.setNextFocusDownId(R.id.et1A9l);

		spModulares.setEnabled(false);
		cbOtro.setChecked(true);

		obj = capitulo.ObtenerCodModular(id_local, Nro_Pred, Nro_IE, nro);

		if (obj != null) {
			et1A9a.setText(obj.getP1_A_2_9A_CMod());
			et1A9b.setText(obj.getP1_A_2_9B_CodLocal());
			sp1A9c.setSelection(obj.getP1_A_2_9C_Nivel());

			if (obj.getSwCompleto() == 1) {
				et1A9f.setText(obj.getP1_A_2_9F_CantAnex() + "");
				sp1A9e.setSelection(obj.getP1_A_2_9E_NroAnex() + 1);
			}

			if (obj.getP1_A_2_9G_T1_Talu() != 0) {
				et1A9g.setText(obj.getP1_A_2_9G_T1_Talu() + "");
			}
			if (obj.getP1_A_2_9H_T1_Taul() != 0) {
				et1A9h.setText(obj.getP1_A_2_9H_T1_Taul() + "");
			}
			if (obj.getP1_A_2_9I_T2_Talu() != 0) {
				et1A9i.setText(obj.getP1_A_2_9I_T2_Talu() + "");
			}
			if (obj.getP1_A_2_9J_T2_Taul() != 0) {
				et1A9j.setText(obj.getP1_A_2_9J_T2_Taul() + "");
			}
			if (obj.getP1_A_2_9K_T3_Talu() != 0) {
				et1A9k.setText(obj.getP1_A_2_9K_T3_Talu() + "");
			}
			if (obj.getP1_A_2_9L_T3_Taul() != 0) {
				et1A9l.setText(obj.getP1_A_2_9L_T3_Taul() + "");
			}
			sp1A9d.setSelection(obj.getP1_A_2_9D_Car());
		}
	}

	public void GuardarData() {
		if (cbOtro.isChecked()) {
			obj.setP1_A_2_9A_CMod(et1A9a.getText().toString().trim());
		} else {
			obj.setP1_A_2_9A_CMod(spModulares.getItemAtPosition(
					spModulares.getSelectedItemPosition()).toString());
		}
		obj.setP1_A_2_9B_CodLocal(et1A9b.getText().toString().trim());
		obj.setP1_A_2_9C_Nivel(sp1A9c.getSelectedItemPosition());
		obj.setP1_A_2_9D_Car(sp1A9d.getSelectedItemPosition());
		obj.setP1_A_2_9E_NroAnex(sp1A9e.getSelectedItemPosition() - 1);

		if (spModulares.getSelectedItemPosition() == 0
				&& (cbOtro.isChecked() == false)) {
			Toast.makeText(this,
					"Debe seleccionar un C\u00f3digo Modular de la lista",
					Toast.LENGTH_SHORT).show();
		} else if (et1A9a.getText().toString().trim().equals("")
				&& cbOtro.isChecked()) {
			et1A9a.requestFocus();
			Toast.makeText(this, "Debe ingresar el C\u00f3digo Modular",
					Toast.LENGTH_SHORT).show();
		} else if (et1A9a.getText().toString().trim().length() != 7
				&& cbOtro.isChecked()) {
			et1A9a.requestFocus();
			Toast.makeText(this,
					"Debe ingresar los 7 d\u00edgitos del C\u00f3digo Modular",
					Toast.LENGTH_SHORT).show();
		} else if (et1A9b.getText().toString().trim().equals("")) {
			et1A9b.requestFocus();
			Toast.makeText(this,
					"Debe ingresar el C\u00f3digo de local escolar",
					Toast.LENGTH_SHORT).show();
		} else if (et1A9b.getText().toString().trim().length() != 6) {
			et1A9b.requestFocus();
			Toast.makeText(
					this,
					"Debe ingresar los 6 d\u00edgitos del C\u00f3digo de local escolar",
					Toast.LENGTH_SHORT).show();
		} else if (sp1A9c.getSelectedItemPosition() == 0) {
			sp1A9c.requestFocus();
			Toast.makeText(this, "Debe seleccionar el Nivel o Modalidad",
					Toast.LENGTH_SHORT).show();
		} else if (sp1A9d.getSelectedItemPosition() == 0) {
			sp1A9d.requestFocus();
			Toast.makeText(this, "Debe seleccionar la caracter\u00edstica",
					Toast.LENGTH_SHORT).show();
		} else if (sp1A9e.getSelectedItemPosition() == 0) {
			sp1A9e.requestFocus();
			Toast.makeText(this, "Debe seleccionar n\u00famero de anexo",
					Toast.LENGTH_SHORT).show();
		} else if (sp1A9e.getSelectedItemPosition() == 1
				&& et1A9f.getText().toString().length() == 0) {
			et1A9f.requestFocus();
			Toast.makeText(this, "Debe ingresar la cantidad de anexos",
					Toast.LENGTH_SHORT).show();
		}/*
		 * else if (et1A9g.getText().toString().trim().length() != 0 &&
		 * et1A9h.getText().toString().trim().length() == 0) {
		 * et1A9h.requestFocus(); Toast.makeText(this,
		 * "Debe ingresar el total de aulas del 1er turno",
		 * Toast.LENGTH_SHORT).show(); } else if
		 * (et1A9g.getText().toString().trim().length() == 0 &&
		 * et1A9h.getText().toString().trim().length() != 0) {
		 * et1A9g.requestFocus(); Toast.makeText(this,
		 * "Debe ingresar el total de alumnos del 1er turno",
		 * Toast.LENGTH_SHORT).show(); } else if
		 * (et1A9g.getText().toString().trim().length() != 0 &&
		 * et1A9h.getText().toString().trim().length() != 0 &&
		 * Integer.parseInt(et1A9g.getText().toString().trim()) <= Integer
		 * .parseInt(et1A9h.getText().toString().trim())) {
		 * et1A9h.requestFocus(); Toast.makeText( this,
		 * "El n\u00famero de alumnos debe ser mayor al n\u00famero de aulas del 1er turno"
		 * , Toast.LENGTH_SHORT).show(); }else if
		 * (et1A9g.getText().toString().trim().length() != 0 &&
		 * et1A9h.getText().toString().trim().length() != 0 && Integer
		 * .parseInt(et1A9h.getText().toString().trim())==0) {
		 * et1A9h.requestFocus(); Toast.makeText( this,
		 * "El total de aulas para el 1er turno debe ser mayor a 0",
		 * Toast.LENGTH_SHORT).show(); } else if
		 * (et1A9i.getText().toString().trim().length() != 0 &&
		 * et1A9j.getText().toString().trim().length() == 0) {
		 * et1A9j.requestFocus(); Toast.makeText(this,
		 * "Debe ingresar el total de aulas del 2do turno",
		 * Toast.LENGTH_SHORT).show(); } else if
		 * (et1A9i.getText().toString().trim().length() == 0 &&
		 * et1A9j.getText().toString().trim().length() != 0) {
		 * et1A9i.requestFocus(); Toast.makeText(this,
		 * "Debe ingresar el total de alumnos del 2do turno",
		 * Toast.LENGTH_SHORT).show(); } else if
		 * (et1A9i.getText().toString().trim().length() != 0 &&
		 * et1A9j.getText().toString().trim().length() != 0 &&
		 * Integer.parseInt(et1A9i.getText().toString().trim()) <= Integer
		 * .parseInt(et1A9j.getText().toString().trim())) {
		 * et1A9i.requestFocus(); Toast.makeText( this,
		 * "El n\u00famero de alumnos debe ser mayor al n\u00famero de aulas del 2do turno"
		 * , Toast.LENGTH_SHORT).show(); }else if
		 * (et1A9i.getText().toString().trim().length() != 0 &&
		 * et1A9j.getText().toString().trim().length() != 0 && Integer
		 * .parseInt(et1A9j.getText().toString().trim())==0) {
		 * et1A9j.requestFocus(); Toast.makeText( this,
		 * "El total de aulas para el 2do turno debe ser mayor a 0",
		 * Toast.LENGTH_SHORT).show(); } else if
		 * (et1A9k.getText().toString().trim().length() != 0 &&
		 * et1A9l.getText().toString().trim().length() == 0) {
		 * et1A9l.requestFocus(); Toast.makeText(this,
		 * "Debe ingresar el total de aulas del 3er turno",
		 * Toast.LENGTH_SHORT).show(); } else if
		 * (et1A9k.getText().toString().trim().length() == 0 &&
		 * et1A9l.getText().toString().trim().length() != 0) {
		 * et1A9k.requestFocus(); Toast.makeText(this,
		 * "Debe ingresar el total de alumnos del 3er turno",
		 * Toast.LENGTH_SHORT).show(); } else if
		 * (et1A9k.getText().toString().trim().length() != 0 &&
		 * et1A9l.getText().toString().trim().length() != 0 &&
		 * Integer.parseInt(et1A9k.getText().toString().trim()) <= Integer
		 * .parseInt(et1A9l.getText().toString().trim())) {
		 * et1A9k.requestFocus(); Toast.makeText( this,
		 * "El n\u00famero de alumnos debe ser mayor al n\u00famero de aulas del 3er turno"
		 * , Toast.LENGTH_SHORT).show(); } else if
		 * (et1A9k.getText().toString().trim().length() != 0 &&
		 * et1A9l.getText().toString().trim().length() != 0 && Integer
		 * .parseInt(et1A9l.getText().toString().trim())==0) {
		 * et1A9l.requestFocus(); Toast.makeText( this,
		 * "El total de aulas para el 3er turno debe ser mayor a 0",
		 * Toast.LENGTH_SHORT).show(); } else
		 * if(et1A9g.getText().toString().trim().length() == 0 &&
		 * et1A9i.getText().toString().trim().length() == 0 &&
		 * et1A9k.getText().toString().trim().length() == 0){
		 * 
		 * Toast.makeText( this,
		 * "Debe ingresar los datos como m\u00ednimo un turno",
		 * Toast.LENGTH_SHORT).show(); }
		 */else {

			if (!et1A9f.getText().toString().trim().equals("")) {
				obj.setP1_A_2_9F_CantAnex(Integer.parseInt(et1A9f.getText()
						.toString().trim()));
			}
			if (!et1A9g.getText().toString().trim().equals("")) {
				obj.setP1_A_2_9G_T1_Talu(Integer.parseInt(et1A9g.getText()
						.toString().trim()));
			}
			if (!et1A9h.getText().toString().trim().equals("")) {
				obj.setP1_A_2_9H_T1_Taul(Integer.parseInt(et1A9h.getText()
						.toString().trim()));
			}
			if (!et1A9i.getText().toString().trim().equals("")) {
				obj.setP1_A_2_9I_T2_Talu(Integer.parseInt(et1A9i.getText()
						.toString().trim()));
			}
			if (!et1A9j.getText().toString().trim().equals("")) {
				obj.setP1_A_2_9J_T2_Taul(Integer.parseInt(et1A9j.getText()
						.toString().trim()));
			}
			if (!et1A9k.getText().toString().trim().equals("")) {
				obj.setP1_A_2_9K_T3_Talu(Integer.parseInt(et1A9k.getText()
						.toString().trim()));
			}
			if (!et1A9l.getText().toString().trim().equals("")) {
				obj.setP1_A_2_9L_T3_Taul(Integer.parseInt(et1A9l.getText()
						.toString().trim()));
			}
			obj.setSwCompleto(1);

			List<P1_A_2_9N> listaAnexos = capitulo.ObtenerListadoAnexosA(
					id_local, Nro_Pred, Nro_IE, obj.getP1_A_2_9_NroCMod());

			if (listaAnexos == null) {
				if (capitulo.GuardarCodModular(obj)) {
					Toast.makeText(this, "Se guardo con \u00e9xito",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(this, "No se pudo guardar",
							Toast.LENGTH_SHORT).show();
				}
			} else {
				if (listaAnexos.size() <= obj.getP1_A_2_9F_CantAnex()) {
					if (capitulo.GuardarCodModular(obj)) {
						Toast.makeText(this, "Se guardo con \u00e9xito",
								Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(this, "No se pudo guardar",
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(this,
							"No puede ingresar una cantidad menor de Anexos",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_guardar_modulares, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnGuardar:
			GuardarData();
			break;

		case R.id.mnFoto:
			Intent foto = new Intent(CapituloI_SeccionA_Modular_Activity.this,
					CapituloIX_Activity.class);
			b.putBoolean("pregunta", true);
			foto.putExtras(b);
			startActivity(foto);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent irListado = new Intent(this,
				CapituloI_SeccionA_Lista_Modulares_Activity.class);
		irListado.putExtras(b);
		startActivity(irListado);
		finish();
	}
}
